<template>
  <div class="main_box">
    <el-row class="header-row">
      <el-col>
        <div class="search_wrap">
          <el-form :inline="true" :model="filters" class="demo-form-inline" ref="searchForm">
            <el-form-item prop="state">
              <el-select v-model="filters.state" :clearable="true" placeholder="状态">
                <el-option v-for="item in dataStatue" :key="item.value" :label="item.label" :value="item.value"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button icon="el-icon-search" type="primary" @click="onSearch">查询</el-button>
              <el-button icon="el-icon-refresh-left" @click="resetForm">重置</el-button>
            </el-form-item>
          </el-form>
        </div>
      </el-col>
      <el-col>
        <div class="opration_wrap">
          <el-tooltip content="刷新" placement="top">
            <el-button circle icon="el-icon-refresh-right" @click="reload" size="mini"></el-button>
          </el-tooltip>
          <el-button type="primary" plain icon="fa fa-plus" v-has="permissionAdd" @click="handleClickAdd()" size="mini"> 新增</el-button>
        </div>
      </el-col>
    </el-row>

    <el-row ref="tableRow">
      <!-- 列表 -->
      <el-table :data="dataList" :fit="true" highlight-current-row>
        <el-table-column type="index" label="序号" width="50" align="center"></el-table-column>
        <!--<el-table-column prop="adviceType" label="类型" :formatter="formatAdviceType"></el-table-column>-->
        <el-table-column prop="createUserName" label="创建人" align="center" width="120"></el-table-column>
        <el-table-column prop="linkName" label="联系人" :formatter="formatLink" align="center" width="100"></el-table-column>
        <el-table-column prop="linkPhone" label="联系方式" :formatter="formatLink" width="120" align="center"></el-table-column>
        <el-table-column prop="diseasesType" label="疾病类型" width="150"></el-table-column>
        <el-table-column prop="content" label="学习内容"></el-table-column>
        <el-table-column prop="answers" label="回复结果" :formatter="formatAnswers" width="160"></el-table-column>
        <el-table-column label="处理状态" width="100">
          <template slot-scope="scope">
            <span>
              <el-tag size="mini" effect="dark" :type="formatStates(scope.row.state)">
                {{ formatState(scope.row.state) }}
              </el-tag>
            </span>
          </template>
        </el-table-column>
        <el-table-column prop="createDate" label="创建时间" width="180"></el-table-column>
        <el-table-column label="操作" width="136">
          <template slot-scope="scope">
            <el-button v-has="permissionDelete" @click="handleClickDelete(scope.row)" size="mini">删除</el-button>
            <el-button v-has="permissionAnswer" v-if="scope.row.state == 0" @click="handleClickAnswer(scope.row)" size="mini">回复</el-button>
            <!--<el-button v-has="permissionCheck" @click="handleClickDetail(scope.row)" size="mini">查看</el-button>
            <el-dropdown trigger="click" size="small">
              <el-button type="primary">
                <i class="fa fa-list-ul"></i>更多
              </el-button>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item v-has="permissionEdit" @click.native="handleClickEditor(scope.row)">编辑</el-dropdown-item>
                <el-dropdown-item v-has="permissionUpStatus" @click.native="handleIsDisable(scope.row)">{{scope.row.state == 0? '启用' : '禁用'}}</el-dropdown-item>
                <el-dropdown-item v-has="permissionDelete" v-if="scope.row.state == 0" @click.native="handleClickDelete(scope.row)">删除</el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>-->
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页工具条 -->
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page.pageNo"
        :page-sizes="[20, 50, 100]"
        :page-size="page.pageSize"
        layout="total, sizes, jumper, next, pager, prev"
        :total="page.pageTotal"
      ></el-pagination>
    </el-row>

    <answers ref="answers" :id="adviceId" @listData="listData"></answers>
  </div>
</template>
<script>
import answers from '@/components/Answers';
import * as message from '@/constants/message';
import { FORMAT_DATE_YYYY_MM_DD } from '@/constants/constants';
import { mapGetters, mapActions } from 'vuex';
import UserApi from '@/api/user/user';

export default {
  name: 'UserStudyManagement',
  inject: ['reload'], //注入局部刷新
  data() {
    let _this = this;
    return {
      dataList: [],
      adviceId: null,
      filters: {
        adviceType: '',
        state: '',
      },
      page: {
        pageNo: 1,
        pageTotal: 0,
        pageSize: 20,
      },
      adviceTypeList: [
        { label: '建议', value: '1' },
        { label: '投诉', value: '2' },
      ],
      dataStatue: [
        { label: '处理中', value: 0 },
        { label: '已处理', value: 1 },
        { label: '已删除', value: -1 },
      ],

      permissionAdd: 'SYSTEM_MANAGER_USER_STUDY_LIST_ADD',
      permissionCheck: 'SYSTEM_MANAGER_EQUIP_SPECIMEN_LIST_SEE',
      permissionEdit: 'SYSTEM_MANAGER_EQUIP_SPECIMEN_LIST_EDIT',
      permissionAnswer: 'SYSTEM_MANAGER_USER_STUDY_LIST_ANSWER',
      permissionDelete: 'SYSTEM_MANAGER_USER_STUDY_LIST_DELETE',
    };
  },
  computed: {
    ...mapGetters([]),
  },
  watch: {
    //to去哪，from从哪来
    $route(to, from) {
      if (from.path == '/user/userStudyAdd') {
        this.listData();
      }
    },
  },
  components: {
    answers,
  },
  mounted() {
    this.init();
    window.addEventListener('keydown', this.keyDown);
  },
  activated() {
    if (this.$route.meta.keepAlive) {
      this.listData();
    }
  },
  destroyed() {
    window.removeEventListener('keydown', this.keyDown, false);
  },
  methods: {
    init() {
      let _this = this;
      _this.listData();
      _this.getTableHeight();
    },
    keyDown(e) {
      //如果是回车则执行查询方法
      if (e.keyCode == 13) {
        this.onSearch();
      }
    },


    resetForm() {
      this.$refs.searchForm.resetFields();
      this.onSearch();
    },
    formatAnswers(row, column, cellValue) {
      if (!cellValue) {
        return '等待回复..';
      } else {
        return cellValue;
      }
    },
    formatAdviceType(row, column, cellValue) {
      if (cellValue == 1) {
        return '建议';
      } else if (cellValue == 2) {
        return '投诉';
      }
    },
    formatState(cellValue) {
      if (cellValue == -1) {
        return '已删除';
      } else if (cellValue == 0) {
        return '处理中';
      } else if (cellValue == 1) {
        return '已处理';
      }
    },
    formatStates(cellValue) {
      if (cellValue == -1) {
        return 'danger';
      } else if (cellValue == 0) {
        return '';
      } else if (cellValue == 1) {
        return 'success';
      }
    },
    formatLink(row, column, cellValue) {
      if (!cellValue) {
        return '未填写';
      } else {
        return cellValue;
      }
    },
    formatDate(row, column, cellValue) {
      let _this = this;
      return _this.$Utils.formatDate(cellValue, FORMAT_DATE_YYYY_MM_DD);
    },
    formatDateTime(row, column, cellValue) {
      let _this = this;
      return _this.$Utils.formatDate(cellValue, 'yyyy-MM-dd hh:mm:ss');
    },
    handleSizeChange(size) {
      let _this = this;
      _this.page.pageSize = size;
      _this.listData();
    },
    handleCurrentChange(val) {
      let _this = this;
      _this.page.pageNo = val;
      _this.listData();
    },
    handleClickAdd() {
      let _this = this;
      _this.$router.push('/user/userStudyAdd');
    },
    handleClickEditor(row) {
      let _this = this;
      _this.$router.push({
        path: '/equip/specimenEdit',
        query: { id: row.esId },
      });
    },
    handleClickDetail(row) {
      let _this = this;
      _this.$router.push({
        path: '/equip/specimenDetail',
        query: { id: row.esId },
      });
    },
    handleClickAnswer(row) {
      let _this = this;
      _this.adviceId = row.adviceId;
      _this.$refs.answers.userAdvice();
      _this.$refs.answers.handleCreate();
    },
    handleClickDelete(val) {
      let _this = this;
      let cond = {
        adviceId: val.adviceId,
        state: -1,
      };
      _this.$confirm(message.MSG_CONFIRM_DELETE, message.TITLE_PROMPT, {}).then(() => {
        UserApi.deleteUserAdvice(cond).then((res) => {
          _this.listData();
          this.$Utils.notifySuccess();
        });
      });
    },
    onSearch() {
      this.page.pageNo = 1;
      this.listData();
    },
    listData() {
      let _this = this;
      let cond = {
        pageNo: _this.page.pageNo,
        pageSize: _this.page.pageSize,
        state: _this.filters.state,
        adviceType: 4,
      };
      UserApi.listUserAdvice(cond).then((res) => {
        _this.dataList = res.data.list;
        _this.page.pageTotal = res.data.total;
      });
    },
  },
};
</script>
<style lang="scss" src="@/assets/css/views/content.scss" scope></style>
